<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TaskRequest;
use App\Models\Task;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\TaskComment;
use App\Repos\TaskRepo;
use App\Services\Wechat;
use App\Exports\TaskExport;
use Maatwebsite\Excel\Facades\Excel;
use DB;

class TaskController extends Controller
{
    public function index(Request $request)
    {
        $where = $param = [];
        
        if($request->status) {
            $where['status'] = (int)$request->status;
        }

        if ($request->mobile) {
            $where['mobile'] = ['like','%'.$request->mobile.'%'];
        }
        if($request->worker_id){
            $where['worker_id'] = $request->worker_id;
        }

        if(session('admin')->role_id!=999) {
             $where['depart_id'] = session('admin')->depart_id;
        }
        $workers = User::where('role_id',1)->get();
    	$list = Task::where($where)->orderBy('created_at','desc')->paginate(20);
    	return view('admin.task.index',compact('list','param','workers'));
    }

    public function show($id)
    {
    	$data = Task::find($id);
        $wechatInfo = Wechat::getUserInfo($data->openid);
        $process = TaskProcess::where('task_id',$id)->get();
        $comment = TaskComment::where('task_id',$id)->first();
    	return view('admin.task.show',compact('wechatInfo','data','process','comment'));
    }

    public function print($id)
    {
        $data = Task::find($id);
       
        $process = TaskProcess::where('task_id',$id)->get();
        $comment = TaskComment::where('task_id',$id)->first();
        return view('admin.task.print',compact('data','process','comment'));
    }

    

    

    public function add()
    {
        $platforms = Platform::select('id','name')->get();
        $sellers   = Seller::select('id','truename')->get();
        return view('admin.task.add',compact('platforms','sellers'));
    }

    public function save(TaskRequest $request)
    {
        $rs['status'] = 'danger';
        $seller_id    = $request->seller_id;
        $seller       = Seller::find($seller_id);
       
        $data     = $request->except('_token');
        $platform = Platform::find($data['platform_id']);
        //dd($data['platform_id']);
        // 如果任务数乘以任务金额+任务数*佣金+平台抽成>卖家余额 那么不能发任务
        $totalMoney    = ($data['price']+$data['commission'])*$data['num'];

       

        DB::beginTransaction();
        try{
           $data['is_admin'] = 1;
           $flag = Task::create($data);       
            
            if(!$flag) {
                DB::rollBack();
                $rs['msg'] = '添加任务失败';
                return back()->with($rs);
            } 
        } catch(\Exception $e) {
            DB::rollBack();
            $rs['msg'] = $e->getMessage();
            return back()->withInput()->with('rs',$rs);
        }
        
        DB::commit();
        $rs['status'] = 'success';
        $rs['msg']    = '任务发布成功';
        return redirect('zadmin/task/')->withInput()->with('rs',$rs);
    }

    
    public function del($user_id)
    {
        $rs['status'] = 'warning';
        if(session('admin')->role_id!=999) {
            $rs['msg']    = '没有操作权限';
            return back()->with('rs',$rs);
        }
        $flag = Task::destroy($user_id);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg']    = '删除成功';
            return redirect('zadmin/task/')->withInput()->with('rs',$rs);
        }
    }

    /**
    * 后台操作订单完成
    */
   public function complete(Request $request)
   {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $task   = Task::find($request->task_id);
        $task->status = 100;
        $flag = $task->save();
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/task')->with('rs',$rs);
        }
        return redirect('zadmin/task')->with('rs',$rs);

   }

    //导出工单。
    public function taskExport(Request $request)
    {
        $param = [];
        if(session('admin')->role_id!=999) {
             $param['depart_id'] = session('admin')->depart_id;
        }
        return Excel::download(new TaskExport($param), '工单统计.xlsx');   
    }
}
